package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.EditText;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.context.BaseApp;
import lib.quasar.util.CalendarUtil;
import lib.quasar.util.InputUtil;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 当前类描述信息
 * created by kalu on 2018/5/15 11:59
 */
public final class HealthDialog extends BaseDialog {

    private OnDialogChangeListener listener;

    public HealthDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_health;
    }

    @Override
    protected void initData() {

        final NavigationView menu = findViewById(R.id.dialog_health_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) dismiss();
            else if (isMenu) {
                if (null != listener) {
                    EditText edit = findViewById(R.id.dialog_health_input);
                    final String trim = edit.getText().toString().trim();
                    if (!TextUtils.isEmpty(trim)) {
                        final String year = CalendarUtil.getYears();
                        final String month = CalendarUtil.getMonths();
                        final String day = CalendarUtil.getDays();
                        final String time = BaseApp.getResource().getString(R.string.http_date, year, month, day);
                        listener.onChange(time, trim);
                    }
                }
                cancel();
            }
        });
    }

    @Override
    public void show() {
        super.show();

        final EditText edit = findViewById(R.id.dialog_health_input);
        edit.requestFocus();
        InputUtil.openKeybord(edit);
    }

    @Override
    public void dismiss() {

        final EditText edit = findViewById(R.id.dialog_health_input);
        edit.clearFocus();
        InputUtil.closeKeybord(edit);

        super.dismiss();
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {
        void onChange(String time, String input);
    }
}
